//
//  DSODataModel.m
//  Balance
//
//  Created by oracle on 7/19/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DSODataModel.h"
#import "DSOData.h"

@interface DSODataModel()

@property (nonatomic) NSUInteger numberOfDays;

@end


@implementation DSODataModel

@synthesize dailyDSO = _dailyDSO;
@synthesize allCustomersDSO = _allCustomersDSO;
@synthesize numberOfDays = _numberOfDays;
@synthesize monthlyKeys = _monthlyKeys;
@synthesize customerID = _customerID;

-(NSUInteger)getNumberOfDaysFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
    NSDateComponents *components = [gregorian components:NSDayCalendarUnit 
                                                fromDate:fromDate 
                                                  toDate:toDate 
                                                 options:0];    
    NSInteger days= [components day];
    return days;
}

-(id) init{
    if (self = [super init]){
        //prepare date for DSO Plots, 3 years from today and back
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyyMMdd"];       
        
        
        NSDate *today = [NSDate date];  
        NSString *todayString = [df stringFromDate:today];
        
        NSString *startDateString = [@"2009" stringByAppendingString:[todayString substringWithRange:NSMakeRange(4,4)]];
        NSDate *startDate = [df dateFromString:startDateString];
        
        NSDate *newYearDay2012 = [df dateFromString:@"20120101"];
        NSDate *endOfYear2009 = [df dateFromString:@"20091231"];
        
        NSUInteger daysin2012 = [self getNumberOfDaysFrom:newYearDay2012 to:today] + 1;
        NSUInteger daysin2009 = [self getNumberOfDaysFrom:startDate to:endOfYear2009] + 1;
        
        self.numberOfDays = daysin2009 + 365 + 365 + daysin2012;
        
        NSTimeInterval oneDayIntervals = 24*60*60;
        
        self.dailyDSO = [NSMutableDictionary dictionaryWithCapacity:2000];
        self.allCustomersDSO = [NSMutableDictionary dictionaryWithCapacity:2000];
        
        CGFloat ran1=30;
        CGFloat ran2=40;
        
        for (int d = 0; d < self.numberOfDays; d++){
            NSDate *dsoDate = [today dateByAddingTimeInterval:(-1*oneDayIntervals*d)];
            NSString *stringDSODate = [df stringFromDate:dsoDate];
            
            DSOData *currentCustomer= [[DSOData alloc] init];
            DSOData *allCustomers= [[DSOData alloc] init];
            
            currentCustomer.date = dsoDate;
            allCustomers.date = dsoDate;
            
            currentCustomer.timeIntervalSinceToday =  oneDayIntervals * d;
            allCustomers.timeIntervalSinceToday = oneDayIntervals * d;
            
            currentCustomer.timeIntervalSinceStart = oneDayIntervals * (self.numberOfDays-d-1);  
            allCustomers.timeIntervalSinceStart = oneDayIntervals * (self.numberOfDays-d-1);   
            
            ran1 = (arc4random()%100)*0.01 - (arc4random()%100)*0.01 + ran1 ;            
            currentCustomer.currentValue = [NSNumber numberWithFloat:ran1];
            
            ran2 = (arc4random()%100)*0.02 - (arc4random()%100)*0.02 + ran2;
            allCustomers.currentValue = [NSNumber numberWithFloat:ran2];
            
            
            [self.dailyDSO setObject:currentCustomer forKey:stringDSODate];
            [self.allCustomersDSO setObject:allCustomers forKey:stringDSODate];
                        
        }
        
        
        
        NSArray *dailyDSOKeys = [self.dailyDSO allKeys];
        NSArray *sortedDailyDSOKeys = [dailyDSOKeys sortedArrayUsingSelector:@selector(compare:)];
        
        
        //prepare Prior Year/DSO For all/Best Possible DSO data
        
        for (int d = 0; d < [dailyDSOKeys count]; d++)
        {
            DSOData *currentData = [self.dailyDSO objectForKey:[sortedDailyDSOKeys objectAtIndex:d]];
            DSOData *currentAllCustomersDSO = [self.allCustomersDSO objectForKey:[sortedDailyDSOKeys objectAtIndex:d]];
            
            if (d > 364 )
            {                
                DSOData *priorData = [self.dailyDSO objectForKey:[sortedDailyDSOKeys objectAtIndex:(d-365)]];
                DSOData *allCustomersPriorData = [self.allCustomersDSO objectForKey:[sortedDailyDSOKeys objectAtIndex:(d-365)]];
                currentData.priorYearValue = priorData.currentValue; 
                currentAllCustomersDSO.priorYearValue = allCustomersPriorData.currentValue;
            }
            else 
            {                
                currentData.priorYearValue=[NSNumber numberWithFloat:0.0f];
                currentAllCustomersDSO.priorYearValue = [NSNumber numberWithFloat:0.0f];
                
            }
            
            //prepare DSO Best Possible
            currentData.bestPossibleDSOValue = [NSNumber numberWithFloat:[currentData.currentValue floatValue]+10.0];
            currentAllCustomersDSO.bestPossibleDSOValue = [NSNumber numberWithFloat:[currentAllCustomersDSO.currentValue floatValue]+10.0];            
            //prepare No of Collectors
            currentData.noOfCollectorsValue = [NSNumber numberWithInt:(arc4random()%300)];
            currentAllCustomersDSO.noOfCollectorsValue = [NSNumber numberWithInt:(arc4random()%300)];
        }
        
        
        //prepare data for the Slider Plot
        NSMutableArray *selectedDailyKeys = [NSMutableArray arrayWithCapacity:500];
        
        DSOData *firstDayData = [self.dailyDSO objectForKey:[sortedDailyDSOKeys objectAtIndex:0]];
        int firstDayInterval = firstDayData.timeIntervalSinceStart;
        for(int s=0;s < [dailyDSOKeys count] ; s++)
        {
            DSOData *data = [self.dailyDSO objectForKey:[sortedDailyDSOKeys objectAtIndex:s]];
            int dataInterval = data.timeIntervalSinceStart - firstDayInterval;
            if(dataInterval % (60*60*24*15) == 0)
            {
                [selectedDailyKeys addObject:[sortedDailyDSOKeys objectAtIndex:s]];
            }
        }
        
        self.monthlyKeys = [NSArray arrayWithArray:selectedDailyKeys];
                
    }
    return self;
}



@end
